package com.boyunv.decorator.example02;/*
 *@description
 *  具体文件,抽象文件读取接口的实现类
 *@author boyunv
 *@create 2023/7/16 8:42
 *@version 1.0
 */

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

public class BaseFileLoader implements DataLoader{

    private String  filePath;

    public BaseFileLoader(String filePath) {
        this.filePath = filePath;
    }

    //读
    @Override
    public String read() {

        try {
            String result = FileUtils.readFileToString(new File(filePath), "utf-8");
            return result;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //写
    @Override
    public void write(String data) {
        try {
            FileUtils.writeStringToFile(new File(filePath),data,"utf-8");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
